home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / RadioButton.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  8.9 KB  |  257 lines

  1. /*
  2.  *
  3. */
  4.  
  5. package symantec.itools.db.awt;
  6.  
  7. import java.util.*;
  8. import symjava.sql.*;
  9. import java.lang.*;
  10. import symantec.itools.db.net.*;
  11. import symantec.itools.db.pro.*;
  12.  
  13. public class RadioButton extends java.awt.Checkbox implements ProjLink
  14. {
  15.     /**
  16.      * The ProjBinder object which this component can use to get data from
  17.      * the server, or change data at the database server.
  18.      */
  19.     private ProjBinder m_ProjBinder;
  20.  
  21.     private String  m_Data;     // Keep in synch with database
  22.     private String  m_Input;    // Keep in synch with screen text
  23.     private int     m_treatBlankAs = 0;
  24.     private boolean m_DynamicUpdate = false;
  25.     private boolean m_ignoreStateChange = true;
  26.     
  27.     public RadioButton()
  28.     {
  29.         super.setLabel("<dbAWARE RadioButton>");
  30.     }
  31.  
  32.     public RadioButton(String label, symantec.itools.db.awt.RadioBox rb, boolean state)
  33.     {
  34.         super(label, rb, state);
  35.         setBinding(rb.getRelView(), rb.getProjection());
  36.     }
  37.  
  38.     public void setTreatBlankAs(String blank)
  39.     {
  40.         if (new String(blank).toUpperCase().equals("DEFAULT"))
  41.         {
  42.             m_treatBlankAs = RelationView.SETBLANKTODEFAULT;
  43.         }
  44.         else if (new String(blank).toUpperCase().equals("NULL"))
  45.             {
  46.                 m_treatBlankAs = RelationView.SETBLANKTONULL;
  47.             }
  48.             else if (new String(blank).toUpperCase().equals("BLANK"))
  49.             {
  50.                 m_treatBlankAs = RelationView.SETBLANKTOEMPTY;
  51.             }
  52.     }
  53.  
  54.     public void setBinding(RelationView relView, String projection)
  55.     {
  56.         try
  57.         {
  58.             int projectionNumber = relView.findProjByName(projection);
  59.             relView.bindProj(projectionNumber, this);
  60.         }
  61.         catch (SQLException Ex)
  62.         {
  63.             raiseException(
  64.                 "SQLException from RadioButton.setBinding: "
  65.                 + Ex.getMessage());
  66.         }
  67.     }
  68.  
  69.     /**
  70.      * Implement the ProjLink interface function, init().
  71.      * This function is called when the control is first bound to data.
  72.      *
  73.      * @param binder The ProjBinder binder object which this component can use
  74.      *               to change data at the database server.
  75.      */
  76.     public void init (ProjBinder binder)
  77.     {
  78.         m_ProjBinder = binder;
  79.         setEditable(m_ProjBinder);
  80.     }
  81.  
  82.    /**
  83.      * Implement the ProjLink interface function, notifyDataChange().
  84.      * This function is called when the projection's data changes at the database.
  85.      *
  86.      * @param binder The ProjBinder binder object which this component can use
  87.      *               to get or change data at the database server.
  88.      */
  89.     public void notifyDataChange(ProjBinder binder)
  90.     {
  91.         m_ignoreStateChange = true;
  92.         if (binder != null)
  93.         {
  94.             m_ProjBinder = binder;
  95.         }
  96.  
  97.         String sLabel = new String(getLabel());
  98.         String data = new String();
  99.         try
  100.         {
  101.             if (binder.isReadable() && !binder.isNull())
  102.             {
  103.                 data = binder.getStringValue();
  104.                 if (sLabel.compareTo(data) == 0)
  105.                 {
  106.                     setState(true);
  107.                 }
  108.             }
  109.         }
  110.         catch (SQLException Ex)
  111.         {
  112.             raiseException(
  113.                 "SQLException from RadioButton.notifyDataChange: "
  114.                 + Ex.getMessage());
  115.         }
  116.         catch (java.io.IOException Ex)
  117.         {
  118.             raiseException(
  119.                 "IOException from RadioButton.notifyDataChange: "
  120.                 + Ex.getMessage());
  121.         }
  122.         setEditable(m_ProjBinder);
  123.     }
  124.  
  125.     // When the control loses focus, pass the text from the
  126.     // control to dbANYWHERE.  This keeps dbANYWHERE up to date.
  127.     // Pass the lostfocus event to the base class.  This allows
  128.     // standard lostfocus processing to occur.
  129.     //
  130.     // NOTE: We only pass the data if it has changed.
  131.     /**
  132.      * catch the lost focus notification and pass the new data to the database server.
  133.      *
  134.      * @param evt the lost focus event being caught
  135.      * @param what the object that is losing focus
  136.      */
  137.     public boolean lostFocus(java.awt.Event evt, Object what)
  138.     {
  139.         notifyStateChange();
  140.         return super.lostFocus(evt, what);
  141.     }
  142.  
  143.     public boolean notifySetData(ProjBinder binder)
  144.     {
  145.         return ( notifyStateChange() );
  146.     }
  147.  
  148.     boolean notifyStateChange()
  149.     {
  150.         if (!m_ignoreStateChange)
  151.         {
  152.             java.awt.CheckboxGroup rbg = getCheckboxGroup();
  153.             if (rbg.getCurrent() == this)
  154.             {
  155.                 try
  156.                 {
  157.                     if (m_ProjBinder != null && m_ProjBinder.isWritable())
  158.                     {
  159.                         m_ProjBinder.setValueFromString(getLabel(), 0, m_treatBlankAs);
  160.                     }
  161.                 }
  162.                 catch (SQLException Ex)
  163.                 {
  164.                     System.out.println("SQLException from TextField.notifyInputChange.setString: " 
  165.                     + Ex.getMessage());
  166.                     return false;
  167.                 }
  168.                 catch (java.io.IOException Ex)
  169.                 {
  170.                     System.out.println("IOException from TextField.notifyInputChange.setString: " + Ex.getMessage());
  171.                     return false;
  172.                 }
  173.             }
  174.         }
  175.         return true;
  176.     }
  177.  
  178.     /**
  179.      * Handles the event. Returns true if the event is handled and
  180.      * should not be passed to the parent of this component. The default
  181.      * event handler calls some helper methods to make life easier
  182.      * on the programmer.
  183.      * @param evt the event
  184.      * @see #mouseEnter
  185.      * @see #mouseExit
  186.      * @see #mouseMove
  187.      * @see #mouseDown
  188.      * @see #mouseDrag
  189.      * @see #mouseUp
  190.      * @see #keyDown
  191.      * @see #action
  192.      */
  193.     public boolean handleEvent(java.awt.Event evt) {
  194.         m_ignoreStateChange = false;
  195.         notifyStateChange();
  196.         return super.handleEvent(evt);
  197.     }
  198.  
  199.     void setEditable(ProjBinder binder)
  200.     {
  201.         //Disable change of state if binder is not writeable
  202.         disable();
  203.         try
  204.         {
  205.             if (binder != null)
  206.             {
  207.                 RelationView rv = binder.getRelationView();
  208.                 if (rv != null && (rv.getCurrentRecordState() != Record.RECSTATE_INVALID))
  209.                 {
  210.                     enable();
  211.                 }
  212.             }
  213.         }
  214.         catch (SQLException Ex)
  215.         {
  216.             System.out.println(Ex.getMessage());
  217.         }
  218.     }
  219.  
  220.     //*************************************************************************//
  221.     //                                                                         //
  222.     // FUNCTION: raiseException                                                //
  223.     //                                                                         //
  224.     //  PURPOSE:                                                               //
  225.     //                                                                         //
  226.     //  PARAMETERS:                                                            //
  227.     //                                                                         //
  228.     //  RETURNS:                                                               //
  229.     //                                                                         //
  230.     // COMMENTS:                                                               //
  231.     //                                                                         //
  232.     //*************************************************************************//
  233.  
  234.     void raiseException(String text)
  235.     {
  236.         System.out.println(text);
  237.     }
  238.  
  239.     //*************************************************************************//
  240.     //                                                                         //
  241.     // FUNCTION: setDynamicUpdate                                              //
  242.     //                                                                         //
  243.     //  PURPOSE:                                                               //
  244.     //                                                                         //
  245.     // PARAMETERS:                                                             //
  246.     //                                                                         //
  247.     //  RETURNS:                                                               //
  248.     //                                                                         //
  249.     // COMMENTS:                                                               //
  250.     //                                                                         //
  251.     //*************************************************************************//
  252.  
  253.     public void setDynamicUpdate(boolean update)
  254.     {
  255.         m_DynamicUpdate = update;
  256.     }
  257. }